package com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.activity.GameChoiceListViewSecondActivity;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceBannerViewPagerInfo;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialThirdActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 郎志奇 on 2016/12/27.
 */

public class GameChoiceBannerAdapter extends PagerAdapter {

    private List<GameChoiceBannerViewPagerInfo> data;

    private Context context;

    public GameChoiceBannerAdapter(List<GameChoiceBannerViewPagerInfo> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    /**
     *
     * 单条的item  创建显示的视图
     * 1.返回值改为View
     * 2.定义一个 View ret=null;
     * 3.创建 ImageView
     * 4.ImageView赋值给 ret
     * 5.ret  要手动添加到container 中
     * 6.返回ret
     *
     * @param container
     * @param position
     * @return
     */

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View ret=null;

        ImageView imageView=new ImageView(context);

        imageView.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        Picasso.with(context).load(Contacts.IMAGE_PREFIX+data.get(position%data.size()).getImage()).into(imageView);

        ret=imageView;
        container.addView(ret);

        imageView.setClickable(true);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String id = data.get(position%data.size()).getId();
                String click_type = data.get(position%data.size()).getClick_type();
                if(click_type.equals("topic_info")){

                    Intent intent=new Intent(context, RecommendTopImageSpecialThirdActivity.class);
                    intent.putExtra("id",id);

                    context.startActivity(intent);
                }
                else if(click_type.equals("app_info")){

                    Intent intent=new Intent(context, GameChoiceListViewSecondActivity.class);
                    intent.putExtra("id",id);

                    context.startActivity(intent);
                }


            }
        });

        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }


    /**
     *
     * 1.去掉 super
     * 2.判断 object 是否是View
     * 3.如果是 在 container 中移除
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        if(object instanceof View){

            View view= (View) object;

            container.removeView(view);

        }

    }
}
